package net.maku.qdd.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import net.maku.qdd.common.annotation.OperateLog;
import net.maku.qdd.common.enums.OperateTypeEnum;
import net.maku.qdd.common.utils.PageResult;
import net.maku.qdd.common.utils.Result;
import net.maku.qdd.query.TicketQuery;
import net.maku.qdd.service.TicketService;

import net.maku.qdd.vo.TicketVO;
import net.maku.qdd.vo.TicketDetailVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券
 *
 * @author MAKU babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("/qdd/ticket")
@Tag(name="优惠券")
@AllArgsConstructor
public class TicketController {
    private final TicketService ticketService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('qdd:ticket')")
    public Result<PageResult<TicketVO>> page(@ParameterObject @Valid TicketQuery query){
        PageResult<TicketVO> page = ticketService.page(query);

        return Result.ok(page);
    }


    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('qdd:ticket')")
    public Result<TicketVO> get(@PathVariable("id") Long id){
        TicketVO data = ticketService.get(id);

        return Result.ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
    @PreAuthorize("hasAuthority('qdd:ticket')")
    public Result<String> save(@RequestBody TicketVO vo){
        ticketService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('qdd:ticket')")
    public Result<String> update(@RequestBody @Valid TicketVO vo){
        ticketService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('qdd:ticket')")
    public Result<String> delete(@RequestBody List<Long> idList){
        ticketService.delete(idList);

        return Result.ok();
    }


    @GetMapping("export")
    @Operation(summary = "导出")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PreAuthorize("hasAuthority('qdd:ticket')")
    public void export() {
        ticketService.export();
    }

    // 小程序专用接口 - 不需要权限验证
    @GetMapping("search/code/{ticketCode}")
    @Operation(summary = "根据券码精确搜索优惠券")
    public Result<TicketVO> searchByCode(@PathVariable("ticketCode") Integer ticketCode){
        TicketVO ticket = ticketService.getByTicketCode(ticketCode);
        System.out.println(ticket);
        return Result.ok(ticket);
    }

    @GetMapping("search/merchant")
    @Operation(summary = "根据商家名称模糊搜索优惠券")
    public Result<PageResult<TicketVO>> searchByMerchantName(@RequestParam("merchantName") String merchantName,
                                                           @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                           @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        PageResult<TicketVO> tickets = ticketService.searchByMerchantName(merchantName, page, limit);
        System.out.println(tickets);
        return Result.ok(tickets);
    }

    // 小程序专用API - 返回详细信息
    @GetMapping("detail/code/{ticketCode}")
    @Operation(summary = "根据券码获取优惠券详细信息（包含商家和商品信息）")
    public Result<TicketDetailVO> getDetailByCode(@PathVariable("ticketCode") Integer ticketCode){
        TicketDetailVO ticketDetail = ticketService.getDetailByTicketCode(ticketCode);
        System.out.println(ticketDetail);
        return Result.ok(ticketDetail);
    }

    @GetMapping("detail/merchant")
    @Operation(summary = "根据商家名称搜索优惠券详细信息")
    public Result<PageResult<TicketDetailVO>> searchDetailByMerchantName(@RequestParam("merchantName") String merchantName,
                                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                        @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        PageResult<TicketDetailVO> ticketDetails = ticketService.searchDetailByMerchantName(merchantName, page, limit);
        System.out.println(ticketDetails);
        return Result.ok(ticketDetails);
    }

    @GetMapping("detail/goods")
    @Operation(summary = "根据商品名称搜索优惠券详细信息")
    public Result<PageResult<TicketDetailVO>> searchDetailByGoodsName(@RequestParam("goodsName") String goodsName,
                                                                     @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                     @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        PageResult<TicketDetailVO> ticketDetails = ticketService.searchDetailByGoodsName(goodsName, page, limit);
        System.out.println(ticketDetails);
        return Result.ok(ticketDetails);
    }
}